<?php

class Admin_ResourceController extends Zend_Controller_Action
{	
    private $_params;
	private $_auth;
	private $_acl;
    public function init(){
        $this->_params      = $this->_request->getParams();
        
        $this->_acl         = new My_Acl();
        $this->view->acl    = $this->_acl;
        
        $this->_auth        = $this->_acl->checkAllowed();
        $this->view->auth   = $this->_auth;
    }
    public function indexAction()
    {       
        $modelResource              = new Admin_Model_Resource;
        $resourceList               = $modelResource->getAllData()->toArray();        
        $this->view->resourceList   = $resourceList;
    }
    public function addAction()
    {   
        $form = new Admin_Form_Resource();
        $form->submit->setLabel('Thêm mới');
        $this->view->form = $form;
        if($this->getRequest()->isPost()){
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)){
                $addData['resourceName']    = $form->getValue('resourceName');
                $addData['moduleName']      = strtolower($form->getValue('moduleName'));
                $addData['controllerName']  = strtolower($form->getValue('controllerName'));
                $addData['actionName']      = strtolower($form->getValue('actionName'));
                $addData['detail']          = $form->getValue('detail');
                $addData['ord']             = $form->getValue('ord');
                $addData['menu']            = $addData['moduleName'].'/'.$addData['controllerName'].'/'.$addData['actionName'];                
                
                $modelResource = new Admin_Model_Resource();
                $resourceCheck = $modelResource->getDataCheckUnique(array('moduleName'=>$addData['moduleName'],'controllerName'=>$addData['controllerName'],'actionName'=>$addData['actionName']));
                  
                if(!$newsCatCheck){                    
                    $resourceId = $modelResource->addData($addData);
                    if($resourceId){
                        $this->_helper->redirector('index', 'resource', 'admin');
                    }else{
                        $this->view->mess = "Đã có lỗi xảy ra. Vui lòng thử lại!";
                        $this->view->flag = "error";
                   }
                }else{
                    $this->view->mess = "Resource đã tồn tại !";
                    $this->view->flag = "error"; 
                }                				
            }
        }                                       
    }
    public function editAction()
    {        
        $newsCatId = (int)($this->_getParam('resourceId', 0));        
        $modelResource = new Admin_Model_Resource();        
        $resourceCheck = $modelResource->getDataByResourceId($newsCatId);        
        $form = new Admin_Form_Resource();
        $this->view->form = $form;                       
        if($resourceCheck){            
            if($this->getRequest()->isPost()){
                $formData = $this->getRequest()->getPost();                 
                if ($form->isValid($formData)) {
                    $editData['resourceId']     = $form->getValue('resourceId');
                    $editData['resourceName']   = $form->getValue('resourceName');
                    $editData['moduleName']     = strtolower($form->getValue('moduleName'));
                    $editData['controllerName'] = strtolower($form->getValue('controllerName'));
                    $editData['actionName']     = strtolower($form->getValue('actionName'));                
                    $editData['detail']         = $form->getValue('detail');
                    $editData['ord']            = $form->getValue('ord');                                
                    $editData['menu']           = $editData['moduleName'].'/'.$editData['controllerName'].'/'.$editData['actionName'];
                    
                    $modelNewsCat = new Admin_Model_Resource();                
                    $resourceCheck = $modelNewsCat->getDataCheckUnique(array('moduleName'=>$editData['moduleName'],'controllerName'=>$editData['controllerName'],'actionName'=>$editData['actionName']));
                                                  
                    if(!$resourceCheck || $editData['resourceId'] == $resourceCheck['resourceId']){                        
                        $modelNewsCat->updateData($editData['resourceId'],$editData);                        
                        $this->_helper->redirector('index', 'resource', 'admin');                        
                    }else{
                        $this->view->mess = "Tên hoặc Mã danh mục đã tồn tại. Vui lòng chọn tên khác!";
                        $this->view->flag = "error"; 
                    }                				
                }
            }else{                
                $form->populate($resourceCheck->toArray());                
            }
        }else{
            $this->_helper->redirector('index', 'newscat', 'admin');
        }
    }
    public function deleteAction()
    {
        if($this->getRequest()->isPost()){
            $del = $this->getRequest()->getPost('del');
            if($del == 'Có'){               
                $resourceId = $this->getRequest()->getPost('resourceId');
                $modelResource = new Admin_Model_Resource();                                                                                                
                $modelResource->deleteData($resourceId);                
                // ghi log
            }
            $this->_helper->redirector('index', 'resource', 'admin');
        }else{
            $id = $this->_getParam('resourceId', 0);
            $modelResource = new Admin_Model_Resource();            
            $this->view->resourceCheck = $modelResource->getDataByResourceId($id);
        }
    }               
}







